package com.es.esdemo.entity;

import lombok.Data;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.util.ResourceUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * @description
 * @author yanghanwei[yanghanwei@bjpp.com]
 * @date 19:56 2020-11-30
 */
@Data
public class CmsDoc {

    public static final String IK= "ik_max_word";
    public static final String COMMA= "comma";


    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String TAGS = "tags";
    public static final String CONTENT = "content";
    public static final String UPDATE_TIME = "updateTime";
    public static final String CREATE_TIME = "createTime";

    private Integer id;
    private String title;
    private String tags;
    private String content;
    private Long updateTime;
    private Long createTime;

    /**
     * 构建setting
     * @return
     */
    public static String buildSetting(){
        StringBuffer str = new StringBuffer();
        File jsonFile = null;
        try {
            jsonFile = ResourceUtils.getFile("classpath:cms-setting.json");
            FileInputStream fis = new FileInputStream(jsonFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line;
            while ((line = br.readLine()) != null) {
                str.append(line);
            }
            fis.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(str);
        return str.toString();
    }

    /**
     * 构建Mapping
     * @return
     */
    public static XContentBuilder buildIndexMapping() {
        XContentBuilder contentBuilder = null;
        try {
            contentBuilder = XContentFactory.jsonBuilder();
            contentBuilder.startObject();
            contentBuilder.startObject("properties");
            contentBuilder.startObject(CmsDoc.ID).field("type", "long").field("store", "true").endObject();
            contentBuilder.startObject(CmsDoc.TITLE)
                    .field("analyzer", IK)
                    .field("type", "text")
                    .field("store", "true")
                    .endObject();
            contentBuilder.startObject(CmsDoc.TAGS).field("analyzer", COMMA)
                    .field("type", "text")
                    .field("store", "true")
                    .endObject();
            contentBuilder.startObject(CmsDoc.CONTENT).field("analyzer", IK)
                    .field("type", "text")
                    .field("store", "true")
                    .endObject();
            contentBuilder.startObject(CmsDoc.CREATE_TIME).field("type", "long").field("store", "true").endObject();
            contentBuilder.startObject(CmsDoc.UPDATE_TIME).field("type", "long").field("store", "true").endObject();
            contentBuilder.endObject();
            contentBuilder.endObject();
        }catch (Exception e){

        }
        return contentBuilder;
    }

}
